home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / smaltalk / gnu_st.lha / gnu_st / smalltalk-1.1.1 / test / blocks.st < prev    next >
Text File  |  1991-09-12  |  2KB  |  106 lines

  1. "Test out block operations"
  2.  
  3. "======================================================================
  4. |
  5. | Copyright (C) 1988, 1989  Steven B. Byrne.
  6. | All rights reserved.
  7. |
  8.  ======================================================================"
  9.  
  10. [45]!        "should return nil"
  11.  
  12. ^[45]!            "should return a block"
  13.  
  14. [^#quem] value!        "should return #quem"
  15.  
  16. ^['foo'] value!        "should return 'foo'"
  17.  
  18. ^[:i | i] value: 'juma'!    "should return 'juma'"
  19.  
  20. ^[:i :j| j] value: 12 value: 17!     "should return 17"
  21.  
  22. !Object methodsFor: 'testing'!
  23.  
  24. blockTest1
  25.     [#foo]
  26. !
  27.  
  28. blockTest2
  29.     [^#foo]
  30. !
  31.  
  32. blockTest3
  33.     ^[#bar]
  34. !
  35.  
  36. blockTest4
  37.     ^[^#bar]
  38. !
  39.  
  40. blockTest5: arg
  41.     ^[arg]
  42. !
  43.  
  44. blockTest6: arg
  45.     ^[:i | arg at: i]
  46. !
  47.  
  48. blockTest7: arg
  49.     | temp |
  50.     temp _ (arg at: 4) + 8.
  51.     ^[temp]
  52. !
  53.  
  54. blockTest8: which
  55.     | first second |
  56.     first _ nil blockTest7: #('one' two 3.0 4 $5).
  57.     second _ nil blockTest7: #("You are[,] number" 'six' seven 8.0 9 $A).
  58.     which ifTrue: [ ^first value ]
  59.       ifFalse: [ ^second value]
  60. !
  61.  
  62. "Implements a 'closure'!!!  Smalltalk is AMAZING!!!"
  63. blockTest9: initialValue
  64.     | counter |
  65.     counter _ initialValue.
  66.     ^[:incr | counter _ counter + incr. 
  67.               counter]
  68. !
  69.  
  70. blockTest10: initialValue
  71.     ^[^initialValue]
  72.  
  73. !!
  74.  
  75. ^nil blockTest1!        "should return nil"
  76.  
  77. ^nil blockTest2!        "should return nil"
  78.  
  79. ^nil blockTest3!        "should return a block context"
  80. ^nil blockTest3 value!        "should return #bar"
  81.  
  82. ^nil blockTest4 value!        "should issue an error, we're returning to
  83.                  a non-existent context"
  84.  
  85. ^(nil blockTest5: 'Smalltalk!') value!
  86.                 "should return 'Smalltalk!'"
  87.  
  88. ^(nil blockTest6: #('one' two 3.0 4 $5)) value: 2!
  89.                 "should return #two"
  90.  
  91. ^(nil blockTest7: #('you' are number 6)) value!
  92.                 "should return 14"
  93.  
  94. ^nil blockTest8: true!        "should return 12"
  95. ^nil blockTest8: false!        "should return 17"
  96.  
  97. "Create a block with the initial value of 2"
  98. Smalltalk at: #summingBlock put: (nil blockTest9: 2)!
  99.  
  100. ^summingBlock value: 3!        "should return 5"
  101. ^summingBlock value: 6!        "should return 11"
  102. ^summingBlock value: 2!        "should return 13"
  103.  
  104. ^(nil blockTest10: 3) value!    "should be illegal; we're returning to non-
  105.                  existent parent"
  106.